package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FileData;
import com.ruoyi.system.domain.FileDataLog;
import com.ruoyi.system.domain.Video;
import com.ruoyi.system.domain.VideoVm;
import com.ruoyi.system.mapper.FileDataLogMapper;
import com.ruoyi.system.mapper.FileDataMapper;
import com.ruoyi.system.mapper.VideoMapper;
import com.ruoyi.system.service.VideoService;
import com.ruoyi.system.service.fileDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class VideoServiceImpl implements VideoService
{
    private static final Logger log = LoggerFactory.getLogger(VideoServiceImpl.class);

    @Autowired
    private VideoMapper videoMapper;


    @Override
    public List<Video> selectFileDataList(VideoVm video) {
        String menus = video.getMenus();
        if(StringUtils.isNoneBlank(menus)) {
            List<String> list = new ArrayList<>();
            if (menus.indexOf(",") != -1) {
                String[] split = menus.split(",");

                for (int i = 0; i < split.length; i++) {
                    String s = split[i];
                    list.add(s);
                }

            }else{
                list.add(menus);
            }
            video.setListmenus(list);
        }
        return videoMapper.selectFileDataList(video);
    }

    @Override
    public int insertFileData(Video video) {
        return videoMapper.insert(video);
    }

    @Override
    public int deleteUserByIds(Integer id) {
        Video video=videoMapper.getVideoData(id);
        int i = videoMapper.deleteByPrimaryKey(id);

        // 指定要删除的文件路径
        File file = new File(video.getFileUrl());

        // 检查文件是否存在
        if (file.exists()) {
            // 尝试删除文件
            if (file.delete()) {
                System.out.println("文件删除成功：" + file.getAbsolutePath());
            } else {
                System.out.println("文件删除失败：" + file.getAbsolutePath());
            }
        } else {
            System.out.println("文件不存在：" + file.getAbsolutePath());
        }
        return  i;
    }

    @Override
    public Video getfileNameObj(String originalFilename) {
        return videoMapper.getfileNameObj(originalFilename);
    }

}
